<template>
  <div class="main-content-container">
    <!-- 资料/产品项目 -->
    <div ref="search" class="app-search">
      <el-row>
        <!-- <el-col :span="4">
        <el-select v-model="selectValue" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-col> -->
        <el-col :span="24" align="right" class="addBtnStyle">
          <el-button @click="handleAdd">+ 增加产品</el-button>
        </el-col>
      </el-row>
    <!-- <el-row style="margin-top:20px">
      <el-col :span="6">
        <el-input v-model="requestInfo.name" style="width:50%" placeholder="名称、拼音码、编码" clearable class="input-with-select" />
        <el-button @click="searchBtn">搜索</el-button>
      </el-col>
    </el-row> -->
    </div>
    <div class="box-show marginTop" :style="'height:'+ (innerHeight + 80) + 'px'">
      <el-table :loading="loading" :data="addProjectList" border :row-class-name="tableColor" :height="innerHeight">
        <el-table-column prop="no" label="编号" :show-overflow-tooltip="true" />
        <el-table-column prop="name" label="名称" :show-overflow-tooltip="true" />
        <el-table-column prop="productAttributes" label="产品属性" :show-overflow-tooltip="true" />
        <el-table-column prop="productClassification" label="产品分类" :show-overflow-tooltip="true" />
        <el-table-column prop="usageTimes" label="可用次数" :show-overflow-tooltip="true" />
        <el-table-column prop="price" label="项目价格" :show-overflow-tooltip="true" />
        <el-table-column prop="discount" label="允许折扣下限" :show-overflow-tooltip="true">
          <template slot-scope="{row}">
            <span>
              {{ row.discount?row.discount+'%':'' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" :show-overflow-tooltip="true" />
        <el-table-column label="操作" :show-overflow-tooltip="true" min-width="140">
          <template slot-scope="scope">
            <!-- <el-button type="text" size="small">查看</el-button> -->
            <el-button type="text" size="small" @click="seeDetails(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="editBtn(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="deleteBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-num="params.page" :page-size="params.pageSize" :on-change="handlePaginationChange" :total="total" />
    </div>
    <AddProjects v-if="show" ref="addProjects" :show.sync="show" @submitForm="submitForm" @success="getAddProjectList" />
    <ProjectDeatils ref="projectDeatils" />
  </div>
</template>
<script>
import { tableColor, labelHeigh } from '@/utils/common.js';
import AddProjects from './addProjects';
import ProjectDeatils from './projectDeatils';
export default {
  components: {
    AddProjects,
    ProjectDeatils,
  },
  data() {
    return {
      addProjectList: [],
      options: [],
      params: {
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      },
      total: 0,
      requestInfo: {},
      tableColor,
      innerHeight: null,
      show: false,
      flag: true,
      loading: false,
      selectValue: '',
    };
  },
  mounted() {
    this.getAddProjectList();
    this.innerHeight = labelHeigh(this.$refs.search);
  },
  methods: {
    async getAddProjectList() {
      this.loading = true;
      const res = await this.$api['data/projects/projectsList']({ ...this.params, ...this.requestInfo });
      this.loading = false;
      if (res.code !== 200) return this.$message.error('查询产品列表失败！');
      this.addProjectList = res.data.result;
      this.total = res.data.total;
    },
    handlePaginationChange(val) {
      this.params = { page: val.pageNum, pageSize: val.pageSize };
      this.getAddProjectList();
    },
    // 搜索
    searchBtn() {
      this.getAddProjectList();
    },
    // 添加-保存
    handleAdd() {
      this.show = true;
      this.flag = true;
      this.$nextTick(function() {
        this.$refs.addProjects.addBtn();
      });
    },
    async submitForm(val) {
      val.productClassification = val.productClassification.pop();
      if (this.flag) {
        const res = await this.$api['data/projects/addList']({ ...val });
        if (res.code !== 200) return this.$message.error('新增产品失败！');
        this.$message.success('新增产品成功！');
      } else {
        const res = await this.$api['data/projects/editList']({ ...val });
        if (res.code !== 200) return this.$message.error('编辑产品失败！');
        this.$message.success('编辑产品成功！');
      }
      this.getAddProjectList();
    },
    seeDetails(row){//查看详情
      this.$refs.projectDeatils.projectDeatils(row);
    },
    // 编辑
    editBtn(row) {
      this.show = true;
      this.flag = false;
      this.$nextTick(function() {
        this.$refs.addProjects.editBtn(row);
      });
    },
    // 删除
    async deleteBtn(row) {
      const confirmResult = await this.$confirm('请确认是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除');
      }
      const res = await this.$api['data/projects/deleteList']({ id: row.id });
      // console.log(res);
      if (res.code !== 200) return this.$message.error('删除产品失败！');
      this.$message.success('删除产品成功！');
      this.getAddProjectList();
    },
  },
};
</script>
